<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quartz Test Page</title>
<script src="quartz.js"></script>
<style>
html {
    width : 100%; height:100%; background: #000; color: #CCC; font-family: 'Lucida Console', Monaco, monospace;
    line-height: 18px; font-size:13px;
}
body {margin: 20px;}
.OK {color:#0F0;}
.KO {color:#F00;}
em {opacity: 0.4;}
b{color:#0FF;}
</style>
<script>
var i18n = {
    start : "从?开始",
    to    : "至",
    L1    : "最后一",
    Ln    : "倒数第?",
    N     : "第?个",
    month : {
        span : "每?个月",
        ANY  : "每月",
        dict : ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
        suffix : "的"
    },
    day : {
        unit   : "日",
        span   : "每?天",
        ANY    : "每天",
        tmpl   : "?号",
        suffix : "的",
        W      : "最近的工作日",
        Wonly  : "所有工作日"
    },
    week : {
        unit : "周",
        span : "每隔?周", 
        ANY  : "每周",
        dict : ["周日","周一","周二","周三","周四","周五","周六"],
        suffix : "的每天"
    },
    hour : {
        span  : "每?小时",
        ANY   : "每小时的",
        tmpl  : "?点",
        suffix : ""
    },
    minute : {
        span  : "每?分钟",
        ANY   : "每分钟的",
        tmpl  : "?分",
        suffix : ""
    },
    second : {
        span  : "每?秒钟",
        ANY   : "",
        tmpl  : "?秒"
    }
};
</script>
</head>
<body class="zui-app-wn_browser ui-body" appnm="wn.browser">
<pre id="log"></pre>
<script>
var ePre = document.getElementById("log");
//-------------------------------------------------------
function log(){
    var str = Array.from(arguments).join(" ");
    ePre.innerHTML += (str || "~empty log line~") + "\n";
}
//-------------------------------------------------------
function explain(qz){
    var str = "<b>" + qz + "</b>";
    for(var i=qz.length; i<22; i++)
        str += " ";
    log(str + " : " + Quartz(qz).toText(i18n));
}
//-------------------------------------------------------
var __qz;
function Qz(str){
    var ms0 = Date.now();
    __qz = Quartz(str);
    var ms1 = Date.now();
    log("\nPARSE("+(ms1-ms0)+"ms):");
    explain(__qz);
}
function assert_match_date(expect, ds){
    var c = new Date(ds);
    var ms0 = Date.now();
    var re = __qz.matchDate(c);
    var ms1 = Date.now();
    var str = re == expect ? "<span class=OK>OK" : "<span class=KO>!!";
    str += " (" + (ms1-ms0) + "ms)";
    str += " : " + ds + "</span>";
    str += " : <em>expect [" + expect + "], while actually [" + re + "]</em>";
    log(str);
}
function assert_match_time(expect, ts){
    var ms0 = Date.now();
    var re = __qz.matchTime(ts);
    var ms1 = Date.now();
    var str = re == expect ? "<span class=OK>OK" : "<span class=KO>!!";
    str += " (" + (ms1-ms0) + "ms)";
    str += " : " + ts + "</span>";
    str += " : <em>expect [" + expect + "], while actually [" + re + "]</em>";
    log(str);
}
function hh() {
    var ary = [];
    ary[24 - 1] = null;
    return ary;
}

function mm() {
    var ary = [];
    ary[24 * 60 - 1] = null;
    return ary;
}

function  ss() {
    var ary = [];
    ary[24 * 60 * 60 - 1] = null;
    return ary;   
}
function Fhh() {
    var args = [hh()].concat(Array.from(arguments));
    FILL.apply(this, args);
}
function Fmm() {
    var args = [mm()].concat(Array.from(arguments));
    FILL.apply(this, args);
}
function Fss() {
    var args = [ss()].concat(Array.from(arguments));
    FILL.apply(this, args);
}
// array, ds, String qzs, String... expect
function FILL() {
    var array  = arguments[0];
    var ds     = arguments[1];
    var qzs    = arguments[2];
    var expect = arguments[3];
    var off    = arguments.length>4 ? arguments[4] : 0;
    var len    = arguments.length>5 ? arguments[5] : array.length;
    var unit   = arguments.length>6 ? arguments[6] : parseInt(86400 / array.length);
    // 创建 & 解析
    var qz = Quartz(qzs);

    if("0 0 8-10 * * ?" == qzs){
        console.log("haha")
    }

    // 执行
    var ms0 = Date.now();
    qz.fill(array, "E", new Date(ds), off, len, unit);
    var ms1 = Date.now();
    var qzos = Quartz.compactAll(array);
    var ms2 = Date.now();

    // 输出结果
    var ss = [];

    // 结果可显示
    for (var i=0; i<qzos.length; i++) {
        qzo = qzos[i];
        ss.push(qzo.index+":"+qzo.obj);
    }

    // 验证
    var ok = true;
    if (ss.length != expect.length) {
        ok = false;
    }
    // 继续验证
    else{
        for (var i=0; i<ss.length; i++) {
            if (ss[i] != expect[i]) {
                ok = false;
                break;
            }
        }
    }
    // 打印   
    var str = ok ? "<span class=OK>OK" : "<span class=KO>!!";
    str += " (" + (ms1-ms0) + "+" + (ms2-ms1) + "=" + (ms2-ms0) + "ms)";
    str += " : </span><b>" + qzs + "</b>";
    str += " : <em>expect [" + expect.join(",") + "], while actually [" + ss.join(",") + "]</em>";
    log(str);
}
// int scale, String ds, String[] qzss, String[] expect
function OVERL() {
    var scale  = arguments[0];
    var ds     = arguments[1];
    var qzs    = arguments[2];
    var expect = arguments[3];

    var c = new Date(ds);

    var qos = [];
    qos[scale - 1] = null;

    var ms0 = Date.now();
    for (var i=0; i< qzs.length; i++) {
        var qz = Quartz(qzs[i]);
        qz.overlap(qos, i + ".E", c);
    }
    var ms1 = Date.now();
    // 压缩结果
    var qzos = Quartz.compactAll(qos);
    var ms2 = Date.now();

    // 输出结果
    var ss = [];

    // 结果可显示
    for(var i=0; i<qzos.length; i++){
        var qzo = qzos[i];
        ss.push(qzo.index+":"+qzo.obj.join(","));
    }

    // 验证
    var ok = true;
    if (ss.length != expect.length) {
        ok = false;
    }
    // 继续验证
    else{
        for (var i=0; i<ss.length; i++) {
            if (ss[i] != expect[i]) {
                ok = false;
                break;
            }
        }
    }
    // 打印   
    var str = ok ? "<span class=OK>OK" : "<span class=KO>!!";
    str += " (" + (ms1-ms0) + "+" + (ms2-ms1) + "=" + (ms2-ms0) + "ms)";
    str += " : </span><b>" + qzs + "</b>";
    str += " : <em>expect [" + expect.join(",") + "], while actually [" + ss.join(",") + "]</em>";
    log(str);
}
//-------------------------------------------------------
</script>
</body>
<script>
//.......................................................
log("NUTZ Quartz testing");
log("@author zozoh@gmail.com");
log("@version alpha");
log("@2016");
log(">>");
var ms_begin = Date.now();
log("========================= Test of parsing\n");
explain("0 0 0 * * ?");
explain("0 0 8-11,13-18 * * ?");
explain("0 0 0 7-13 JUL ?");
explain("0 0 0 1,3,5 * ?");
explain("0 0 8/3 * * ?");
explain("0 0 0 4L * ?");
explain("0 0 0 W * ?");
explain("0 0 0 1LW * ?");
explain("0 0 0 6LW * ?");
explain("0 0 0 * * 1-3");
explain("0 0 0 * * 1,2-4,7");
explain("0 0 0 * * FRI#2");
explain("0 0 0 * * 2#4");
explain("0 0/5 8,10-14,23 * * ?");
explain("0 0 0,1 * * ?");
//.......................................................
log("\n========================= Test of matchDate");
Qz("0 0 0 1-5 * ?");
assert_match_date(true,  "2015-07-01");
assert_match_date(true,  "2015-07-05");
assert_match_date(false, "2015-07-06");
assert_match_date(false, "2015-06-30");
// ............................................
Qz("0 0 0 1-5 JUL ?");
assert_match_date(true,  "2015-07-01");
assert_match_date(true,  "2015-07-05");
assert_match_date(false, "2015-07-06");
assert_match_date(false, "2015-06-30");
// ............................................
Qz("0 0 0 1-5 7 ?");
assert_match_date(true,  "2015-07-01");
assert_match_date(true,  "2015-07-05");
assert_match_date(false, "2015-07-06");
assert_match_date(false, "2015-06-30");
// ............................................
Qz("0 0 0 1-3,6-8 * ?");
assert_match_date(true,  "2012-04-02");
assert_match_date(true,  "2014-06-07");
assert_match_date(false, "2011-05-04");
// ............................................
Qz("0 0 0 * 4,6 ?");
assert_match_date(true,  "2012-04-05");
assert_match_date(true,  "2014-06-06");
assert_match_date(false, "2011-05-07");
// ............................................
Qz("0 0 0 * * SUN#2");
assert_match_date(true,  "2012-02-12");
assert_match_date(false, "2012-02-05");
assert_match_date(false, "2012-02-19");
// ............................................
Qz("0 0 0 * * ?");
assert_match_date(true,  "2012-03-15");
// ............................................
Qz("0 0 0 W * ?");
assert_match_date(true,  "2012-02-07");
assert_match_date(false, "2012-02-05");
assert_match_date(false, "2012-02-11");
// ............................................
Qz("0 0 0 * * SUN-MON");
assert_match_date(true,  "2012-02-05");
assert_match_date(true,  "2012-02-06");
assert_match_date(false, "2012-02-07");
// ............................................
Qz("0 0 0 * * 1-2");
assert_match_date(true,  "2012-02-05");
assert_match_date(true,  "2012-02-06");
assert_match_date(false, "2012-02-07");
// ............................................
Qz("0 0 0 * 4-6 ?");
assert_match_date(true,  "2012-04-05");
assert_match_date(true,  "2018-05-06");
assert_match_date(true,  "2016-06-06");
assert_match_date(false, "2012-03-07");
assert_match_date(false, "2012-07-07");
// ............................................
Qz("0 0 0 1 * ?");
assert_match_date(true,  "2012-02-01");
assert_match_date(true,  "2012-03-01");
assert_match_date(false, "2012-02-29");
assert_match_date(false, "2012-05-14");
// ............................................
Qz("0 0 0 1-7 * ?");
assert_match_date(true,  "2012-02-01");
assert_match_date(true,  "2012-03-07");
assert_match_date(false, "2012-02-29");
assert_match_date(false, "2012-05-14");
// ............................................
Qz("0 0 0 1L * ?");
assert_match_date(true,  "2012-02-29");
assert_match_date(true,  "2012-03-31");
assert_match_date(false, "2012-02-28");
assert_match_date(false, "2012-05-14");
//.......................................................
log("\n========================= Test of matchTime");
Qz("* 9/25 * * * ?");
assert_match_time(false, "00:00:00");

assert_match_time(false, "12:08:23");
assert_match_time(true,  "09:09:45");
assert_match_time(false, "12:10:23");

assert_match_time(false, "12:33:23");
assert_match_time(true,  "09:34:45");
assert_match_time(false, "12:35:23");

assert_match_time(false, "12:58:23");
assert_match_time(true,  "09:59:45");
assert_match_time(false, "12:00:23");

assert_match_time(true,  "23:59:59");

// ............................................
Qz("* 1-10 * * * ?");
assert_match_time(true,  "12:01:23");
assert_match_time(false, "00:00:00");
assert_match_time(true,  "09:03:45");
assert_match_time(true,  "15:10:28");
assert_match_time(false, "23:59:59");

// ............................................
Qz("0 1-10 0 * * ?");
assert_match_time(true,  "00:01:00");
assert_match_time(false, "00:00:00");
assert_match_time(true,  "00:03:00");
assert_match_time(true,  "00:10:00");
assert_match_time(false, "23:59:59");

// ............................................
Qz("0 0 0 * * ?");
assert_match_time(true,  "00:00:00");
assert_match_time(false, "00:00:01");
assert_match_time(false, "00:00:02");
assert_match_time(false, "23:59:59");

// ............................................
Qz("* 1,6,10 * * * ?");
assert_match_time(false, "00:00:00");

assert_match_time(false, "12:00:23");
assert_match_time(true,  "12:01:23");
assert_match_time(false, "12:02:23");

assert_match_time(false, "12:05:23");
assert_match_time(true,  "09:06:45");
assert_match_time(false, "12:07:23");

assert_match_time(false, "12:09:23");
assert_match_time(true,  "15:10:28");
assert_match_time(false, "12:11:23");

assert_match_time(false, "23:59:59");
//.......................................................
log("\n========================= Test of fill_by_hour\n");
Fhh("2012-02-08", "0 0 5-8 * * ?", ["5:E", "6:E", "7:E", "8:E"]);
Fhh("2012-02-08", "0,3,4,5 0/1 4-8 * * ?", ["4:E", "5:E", "6:E", "7:E", "8:E"]);

Fhh("2012-02-08", "0 0 5,8 * * ?", ["5:E", "8:E"]);
Fhh("2012-02-08", "0,3,4,5 0/1 5,8 * * ?", ["5:E", "8:E"]);
//.......................................................
log("\n========================= Test of test_fill_by_min\n");
Fmm("2012-02-08", "0 1,2 0,1 * * ?", ["1:E", "2:E", "61:E", "62:E"]);
//.......................................................
log("\n========================= Test of test_fill_by_sec\n");
Fss("2012-02-08", "0,1 0 0,1 * * ?", ["0:E", "1:E", "3600:E", "3601:E"]);
//.......................................................
log("\n========================= Test of test_fill_partly\n");
Fhh("2012-02-08", "0 0 8-10 * * ?", ["9:E", "10:E"], 9, 3);
//.......................................................
log("\n========================= Test of test_overlap_by_hour\n");
OVERL(24,
      "2012-02-08",
      ["0 0 5-7 * * ?", "0 0 5-6 * 2 ?", "0 0 5-8 * 3 ?"],
      ["5:0.E,1.E", "6:0.E,1.E", "7:0.E"]);

OVERL(24,
      "2012-03-08",
      ["0 0 5-7 * * ?", "0 0 5-6 * 2 ?", "0 0 5-8 * 3 ?"],
      ["5:0.E,2.E", "6:0.E,2.E", "7:0.E,2.E", "8:2.E"]);
//.......................................................
log("\n################################################\n");
var ms_end = Date.now();
log("ALL TEST DONE IN " + (ms_end - ms_begin) + "ms");
log("Nutz Quartz Alpha;");
log("  by zozoh @ 2016");
//-------------------------------------------------------
</script>
</html>